import pygame, sys

from universe import Universe
from particle import Particle
from vector import Vector

# force generator imports
from force_generators.earth_force_generator import EarthFG
from force_generators.box_force_generator import BoxFG
from force_generators.collision_force_generator import CollisionFG

# dimensions of the window and screen
WIN_WIDTH = 800
WIN_HEIGHT = 600

universe = Universe()

pygame.init()

window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('GenParticleSim')
screen = pygame.display.get_surface()

def init_universe():
    # make force generators
    # force generators dependant on an accurate velocity should be added first
    universe.add_force_generator(EarthFG(universe.particles, 'Earth'))
    boxfg = BoxFG(universe.particles, 'Box',
            Vector(0, WIN_HEIGHT), Vector(WIN_WIDTH, 0))
    universe.add_force_generator(boxfg)
    universe.add_force_generator(CollisionFG(universe.particles, 'Collision'))
    # make particles
    #universe.add_particle(Particle(5, 550, 100, 1000))

def input(events):
    LEFT = 1
    for event in events:
        if event.type == pygame.QUIT: # close button
            sys.exit(0)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == LEFT: # left click
                print event.pos[0], event.pos[1], event.button
                universe.add_particle(Particle(event.pos[0], WIN_HEIGHT - event.pos[1]))

def draw_universe():
    screen.fill((0, 0, 0))
    for particle in universe.particles:
        # draw each particle, accounting for the fact that
        # pygame's y increases going down
        pygame.draw.circle(screen, (255, 255, 255),
                (int(particle.position.x),
                    int(screen.get_height() - particle.position.y)), 5)
    pygame.display.flip()

init_universe()
while True:
    input(pygame.event.get())
    draw_universe()
    #TODO: use a timer to give an accurate timescale
    universe.update()
    #print universe.get_bounds_of_velocity()
